function E=el2mat_b(fname)
% function E=el2mat_b(fname)
% This function opens an eyelink *.edf file and returns the data.
%
% fname: an *.EDF file from an Eyelink experiment.
%     E: A structure containing the events and eye data from the EDF file.   
%        The following fields are defined:
%         .time:   time stamp for each data point
%         .gazex:  xpos of calibrated gaze
%         .gazey:  xpos of calibrated gaze
%         .pupilx: xpos of uncalibrated pupil
%         .pupily: ypos of uncalibrated pupil
%         .headx:  xpos of uncalibrated head (CR?)
%         .heady:  ypos of uncalibrated head (CR?)
%         .area:   area of pupil 
% 
%         .event.on  - beginning of event
%         .event.off - end of event
%         .event.code- type of event(fixation, saccade, or blink).           
%         .mess.times- the time that message events occured
%         .mess.txt  - the text from message events (in cell format).
%         .preamble  - the header text from the file
%         .codes     - eye event codes that can be used to index the .event.code field.


if nargin==0
    [fname pathname]=uigetfile('*.edf','load EDF file','.edf');
    if fname==0
        disp('>>  canceled by user')
        help(mfilename)
        return
    end
    fname=[pathname fname ];
end

try
    [sampledata eventdata mestimes messages preamble]=geteldata(fname);
catch
    disp(sprintf(['  Error processing file:\n  ' fname '\n  getELdata requires an absolute filename (i.e full path)']));
    E=[];
    return;
end
sampledata=sampledata';
eventdata=eventdata';
mestimes=mestimes';
for i = 1:length(mestimes);
        txt{i,:}=deblank(messages(i,:));
end

i = 1;
E.time  =sampledata(:,i);i=i+1;
E.gazex =sampledata(:,i);i=i+1;
E.gazey =sampledata(:,i);i=i+1;
E.pupilx=sampledata(:,i);i=i+1;
E.pupily=sampledata(:,i);i=i+1;
E.headx =sampledata(:,i);i=i+1;
E.heady =sampledata(:,i);i=i+1;
E.area  =sampledata(:,i);i=i+1;
i = 1;
E.event.on  =eventdata(:,i);i=i+1;
E.event.off =eventdata(:,i);i=i+1;
E.event.code=eventdata(:,i);i=i+1;
E.mess.times=mestimes;
E.mess.txt  =txt;
E.preamble  =preamble;

% fixed eyelink event codes
E.codes.FIX=8;
E.codes.SACC=6;
E.codes.BLINK=4;
% oname=fname;
% oname(end-3)='_';
% save([oname '.mat'] ,'E');

